package top.hisoft.pattern.creational.simplefactory.farm;

import top.hisoft.pattern.creational.simplefactory.farm.exception.BadFruitException;
import top.hisoft.pattern.creational.simplefactory.farm.fruits.Apple;
import top.hisoft.pattern.creational.simplefactory.farm.fruits.Grape;
import top.hisoft.pattern.creational.simplefactory.farm.fruits.Strawberry;

/**
 * 农场工厂，此类会根据客户的要求，创建不同的水果。
 * 如果接到不合法的要求，会抛出 {@link BadFruitException} 异常。
 *
 * @author sky
 * @since 2025-05-30
 */
public class FruitFactory {

    /**
     * 静态工厂方法
     *
     * @param name
     * @return
     * @throws BadFruitException
     */
    public static Fruit createFruit(String name) throws BadFruitException {

        Fruit fruit;
        if ("apple".equalsIgnoreCase(name)) {
            fruit = new Apple();
        } else if ("grape".equalsIgnoreCase(name)) {
            fruit = new Grape();
        } else if ("strawberry".equalsIgnoreCase(name)) {
            fruit = new Strawberry();
        } else {
            throw new BadFruitException("Bad fruit request.");
        }
        return fruit;
    }
}
